﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSLib.Parsing.Common;
using PSLib.Parsing.Common.Search;
using Antlr.Runtime;

namespace PSLib.Parsing.Common
{
    /// <summary>
    /// Class for storing Recognized tokens range
    /// </summary>
    public class RecognizedTokenRange 
    {
        public RecognizedTokenRange(CommonTokenStream stream)
        {
            this.TokenStream = stream;
        }

        public IEnumerable<RecognizedToken> GetEnumeration( bool includeHidden)
        {
            TokensStreamSearchProvider search = this.TokenStream.GetSearchProvider();
            if (this.IsEmpty) yield return null;

            int currentTokenIndex = this.Start.NativeToken.TokenIndex;
            for (int i = this.Start.NativeToken.TokenIndex; i < this.End.NativeToken.TokenIndex + 1; i++)
            {
                RecognizedToken token = search.Recognize(i);
                if (token.NativeToken.Channel != 0)
                {
                    if (includeHidden) yield return token;
                }
                else yield return token;
            }
        }


        public bool IsEmpty
        {
            get { return this.Start == null || this.End == null; }
        }
        public RecognizedToken Start { get; set; }
        public RecognizedToken End { get; set; }

        public CommonTokenStream TokenStream { get; private set; }
    }
}
